package managedbean.stakeholder;

import entity.AddressEntity;
import entity.OperatingHoursEntity;
import entity.CuisineTypeEntity;
import entity.ExtendedOperatingHoursEntity;
import entity.FnBBranchEntity;
import entity.OperatingHoursRemarksEntity;
import entity.StakeholderEntity;
import exception.ExistException;
import java.io.IOException;
import java.io.Serializable;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import session.stateless.BranchSessionBean;
import session.stateless.SystemMessagesBean;
import util.enumeration.CuisineType;
import util.localization.WebLocalization;

/**
 *
 * @author Amelia
 */
@ManagedBean
@ViewScoped
public class DeleteBranchManagedBean implements Serializable {

    @EJB
    private SystemMessagesBean systemMessages;
    //User Selected Locale
    private String preferredLocale;
    private String previousLocale = "";
    private WebLocalization localization;
    @EJB
    private BranchSessionBean branchSessionBean;
    private AddressEntity branchAddressToEdit;
    private OperatingHoursRemarksEntity operatingHoursRemarksToEdit;
    private List<CuisineTypeEntity> cuisinePrefToEdit;
    private FnBBranchEntity fnbBranchToEdit;
    private StakeholderEntity stakeholderToEdit;
    private List<OperatingHoursEntity> branchOperatingHoursToEdit;
    private List<ExtendedOperatingHoursEntity> extendedOperatingHoursList;
    private List<CuisineType> cuisineTypes;
    private String mondayStart;
    private String mondayEnd;
    private String tuesdayStart;
    private String tuesdayEnd;
    private String wednesdayStart;
    private String wednesdayEnd;
    private String thursdayStart;
    private String thursdayEnd;
    private String fridayStart;
    private String fridayEnd;
    private String saturdayStart;
    private String saturdayEnd;
    private String sundayStart;
    private String sundayEnd;
    private boolean branchDeleted;

    /** Creates a new instance of DeleteBranchManagedBean */
    public DeleteBranchManagedBean() {
    }

    public AddressEntity getBranchAddressToEdit() {
        return branchAddressToEdit;
    }

    public void setBranchAddressToEdit(AddressEntity branchAddressToEdit) {
        this.branchAddressToEdit = branchAddressToEdit;
    }

    public OperatingHoursRemarksEntity getOperatingHoursRemarksToEdit() {
        return operatingHoursRemarksToEdit;
    }

    public void setOperatingHoursRemarksToEdit(OperatingHoursRemarksEntity operatingHoursRemarksToEdit) {
        this.operatingHoursRemarksToEdit = operatingHoursRemarksToEdit;
    }

    public List<CuisineTypeEntity> getCuisinePrefToEdit() {
        return cuisinePrefToEdit;
    }

    public void setCuisinePrefToEdit(List<CuisineTypeEntity> cuisinePrefToEdit) {
        this.cuisinePrefToEdit = cuisinePrefToEdit;
    }

    public FnBBranchEntity getFnbBranchToEdit() {
        return fnbBranchToEdit;
    }

    public void setFnbBranchToEdit(FnBBranchEntity fnbBranchToEdit) {
        this.fnbBranchToEdit = fnbBranchToEdit;
    }

    public List<OperatingHoursEntity> getBranchOperatingHoursToEdit() {
        return branchOperatingHoursToEdit;
    }

    public List<ExtendedOperatingHoursEntity> getExtendedOperatingHoursList() {
        return extendedOperatingHoursList;
    }

    public void setExtendedOperatingHoursList(List<ExtendedOperatingHoursEntity> extendedOperatingHoursList) {
        this.extendedOperatingHoursList = extendedOperatingHoursList;
    }

    public List<CuisineType> getCuisineTypes() {
        return cuisineTypes;
    }

    public void setCuisineTypes(List<CuisineType> cuisineTypes) {
        this.cuisineTypes = cuisineTypes;
    }

    public String getMondayStart() {
        return mondayStart;
    }

    public String getMondayEnd() {
        return mondayEnd;
    }

    public String getTuesdayStart() {
        return tuesdayStart;
    }

    public String getTuesdayEnd() {
        return tuesdayEnd;
    }

    public String getWednesdayStart() {
        return wednesdayStart;
    }

    public String getWednesdayEnd() {
        return wednesdayEnd;
    }

    public String getThursdayStart() {
        return thursdayStart;
    }

    public String getThursdayEnd() {
        return thursdayEnd;
    }

    public String getFridayStart() {
        return fridayStart;
    }

    public String getFridayEnd() {
        return fridayEnd;
    }

    public String getSaturdayStart() {
        return saturdayStart;
    }

    public String getSaturdayEnd() {
        return saturdayEnd;
    }

    public String getSundayStart() {
        return sundayStart;
    }

    public String getSundayEnd() {
        return sundayEnd;
    }

    /************************************************************************************\
    /*********************************** METHODS ****************************************\
    /************************************************************************************/
    public void initView(PhaseEvent event) {
        /* LOCALIZATION */
        localization = new WebLocalization();
        preferredLocale = localization.getPreferredLocaleFromSession();
        /* END LOCALIZATION */

        FacesContext.getCurrentInstance().getExternalContext().getSession(true);

        if (fnbBranchToEdit == null) {
            fnbBranchToEdit = (FnBBranchEntity) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("selectedFnBBranch");
        }
        if (branchAddressToEdit == null) {
            branchAddressToEdit = branchSessionBean.viewBranchAddress(fnbBranchToEdit.getId());
        }
        if (cuisinePrefToEdit == null) {
            cuisinePrefToEdit = branchSessionBean.viewCuisinePref(fnbBranchToEdit.getId());
        }
        if (branchOperatingHoursToEdit == null) {
            branchOperatingHoursToEdit = branchSessionBean.viewBranchOperatingHours(fnbBranchToEdit.getId());
        }
        if (extendedOperatingHoursList == null) {
            extendedOperatingHoursList = branchSessionBean.viewExtendedOperatingHours(fnbBranchToEdit.getId());
        }
        if (operatingHoursRemarksToEdit == null || !previousLocale.equals(preferredLocale)) {
            OperatingHoursRemarksEntity operatingHoursRemarks = branchSessionBean.viewOperatingHoursRemarks(preferredLocale, fnbBranchToEdit.getId());
            operatingHoursRemarksToEdit = operatingHoursRemarks;
        }
        previousLocale = preferredLocale;
    }

    public void deleteBranch(ActionEvent event) throws ExistException, IOException {
        branchDeleted = branchSessionBean.deleteBranchDetails(fnbBranchToEdit, cuisinePrefToEdit, branchOperatingHoursToEdit, branchAddressToEdit, extendedOperatingHoursList);
        if (branchDeleted == true) {
            String statusMessage = systemMessages.getSystemMessage("BRANCH_DELETE_SUCCESS", preferredLocale);
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, statusMessage, statusMessage));
            FacesContext.getCurrentInstance().getExternalContext().getFlash().setKeepMessages(true);
            //FacesContext.getCurrentInstance().getExternalContext().redirect("ViewAllBranch.xhtml");
            FacesContext ctx = FacesContext.getCurrentInstance();
            String nextPage = ctx.getExternalContext().encodeActionURL(ctx.getApplication().getViewHandler().getActionURL(ctx, "/stakeholder/ViewAllBranch.xhtml"));
            FacesContext.getCurrentInstance().getExternalContext().redirect(nextPage);
        } else {
            String statusMessage = systemMessages.getSystemMessage("BRANCH_DELETE_NOT_SUCCESSFUL", preferredLocale);
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, statusMessage, statusMessage));
        }
    }

    public StakeholderEntity getStakeholderToEdit() {
        return stakeholderToEdit;
    }

    public void setStakeholderToEdit(StakeholderEntity stakeholderToEdit) {
        this.stakeholderToEdit = stakeholderToEdit;
    }
}
